//
//  HKHomeViewController.m
//  AQBPolice
//
//  Created by apple on 2017/7/5.
//  Copyright © 2017年 eight. All rights reserved.
//

//    self.view.backgroundColor = HKColor(74, 79, 109);

//    // 创建视图控制器
//    UIViewController *meVC = [[UIStoryboard storyboardWithName:@"Me" bundle:nil] instantiateInitialViewController];
//    UIViewController *secureVC = [[UIStoryboard storyboardWithName:@"Secure" bundle:nil] instantiateInitialViewController];
//
//    LBSegmentControl * segmentControl = [[LBSegmentControl alloc] initStaticTitlesWithFrame:CGRectMake(0, 20, kScreen_width, 45)];
//    segmentControl.titles = @[@"我的", @"安全宝"];
//    segmentControl.viewControllers = @[meVC, secureVC];
//    segmentControl.titleNormalColor = [UIColor whiteColor];
//    segmentControl.titleSelectColor = [UIColor orangeColor];
//    [segmentControl setBottomViewColor:[UIColor orangeColor]];
//    segmentControl.isTitleScale = YES;
//
//
//    [self.view addSubview:segmentControl];

#import "HKHomeViewController.h"
#import "LBSegmentControl.h"

#import "HKMeViewController.h"
#import "HKSecureViewController.h"

@interface HKHomeViewController ()<UIScrollViewDelegate>
//两个按钮
@property (weak, nonatomic) IBOutlet UIButton *meButton;
@property (weak, nonatomic) IBOutlet UIButton *anquanbaoButton;
//下面的横条
@property (weak, nonatomic) IBOutlet UIView *meEditView;
@property (weak, nonatomic) IBOutlet UIView *anquanbaoEditView;
//安全图标
@property (weak, nonatomic) IBOutlet UIImageView *auquanbaoImageView;
//两个控制器
@property (nonatomic, strong) UIViewController *meVC;
@property (nonatomic, strong) UIViewController *secureVC;

@property (nonatomic, strong) UIScrollView *contentScrollView;//内容scrollView

@end

@implementation HKHomeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 65, kScreen_width, kScreen_height)];
    self.contentScrollView.delegate = self;
    //添加子控制器
    self.meVC = [[UIStoryboard storyboardWithName:@"Me" bundle:nil] instantiateInitialViewController];
    self.secureVC = [[UIStoryboard storyboardWithName:@"Secure" bundle:nil] instantiateInitialViewController];
    
    [self addChildViewController:self.meVC];
    [self addChildViewController:self.secureVC];
    
    //设置滑动视图的contentSize
    self.contentScrollView.contentSize = CGSizeMake(2 * kScreen_width, 0);
    //取消弹簧效果
    self.contentScrollView.bounces = NO;
    self.contentScrollView.pagingEnabled = YES;
    //隐藏水平滚动条
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    
    //显示安全宝的控制器
    self.secureVC.view.frame = CGRectMake(kScreen_width, 0, kScreen_width, kScreen_height);
    self.meVC.view.frame = CGRectMake(0, 0, kScreen_width, kScreen_height);
    self.contentScrollView.contentOffset = CGPointMake(kScreen_width, 0);
    [self.contentScrollView addSubview:self.meVC.view];
    [self.contentScrollView addSubview:self.secureVC.view];
    
    //禁用安全宝按钮，颜色橙色
    self.anquanbaoButton.enabled = NO;
    [self.anquanbaoButton setTitleColor:HKColor(253, 151, 39) forState:UIControlStateDisabled];
    //我的下面的横条隐藏
    self.meEditView.hidden = YES;
    //安全宝下面的横条变色，换图片
    self.anquanbaoEditView.backgroundColor = HKColor(253, 151, 39);
    self.auquanbaoImageView.image = [UIImage imageNamed:@"DC38CBE6-9589-4D9F-B70F-6604D07134C9"];
    
    [self.view addSubview:self.contentScrollView];
    
}
//我的按钮点击
- (IBAction)meButtonClicked:(UIButton *)button {
    
    //禁用我的按钮，颜色橙色
    self.meButton.enabled = NO;
    [self.meButton setTitleColor:HKColor(253, 151, 39) forState:UIControlStateDisabled];
    //安全宝下面的横条隐藏
    self.anquanbaoEditView.hidden = YES;
    //我的下面的横条变色，换图片
    self.meEditView.hidden = NO;
    self.meEditView.backgroundColor = HKColor(253, 151, 39);
    self.auquanbaoImageView.image = [UIImage imageNamed:@"40D6EF11-FE68-4E24-9AB1-A8BDFFED52BF"];
    //可用安全宝按钮
    self.anquanbaoButton.enabled = YES;
    //安全宝按钮白色
    [self.anquanbaoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    // 取出被点击按钮的索引
    NSInteger index = button.tag;
    // 让底部的内容scrollView滚动到对应位置
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = index * self.contentScrollView.frame.size.width;
    [self.contentScrollView setContentOffset:offset animated:YES];
}
//安全宝按钮点击
- (IBAction)anquanButtonClicked:(UIButton *)button {
    //禁用安全宝按钮，颜色橙色
    self.anquanbaoButton.enabled = NO;
    [self.anquanbaoButton setTitleColor:HKColor(253, 151, 39) forState:UIControlStateDisabled];
    //我的下面的横条隐藏
    self.meEditView.hidden = YES;
    //安全宝下面的横条变色，换图片
    self.anquanbaoEditView.hidden = NO;
    self.anquanbaoEditView.backgroundColor = HKColor(253, 151, 39);
    self.auquanbaoImageView.image = [UIImage imageNamed:@"DC38CBE6-9589-4D9F-B70F-6604D07134C9"];
    //可用我的按钮
    self.meButton.enabled = YES;
    //我的按钮白色
    [self.meButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    // 取出被点击按钮的索引
    NSInteger index = button.tag;
    // 让底部的内容scrollView滚动到对应位置
    CGPoint offset = self.contentScrollView.contentOffset;
    offset.x = index * self.contentScrollView.frame.size.width;
    [self.contentScrollView setContentOffset:offset animated:YES];
}

#pragma mark - <UIScrollViewDelegate>
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    // 一些临时变量
    CGFloat width = scrollView.frame.size.width;
    CGFloat height = scrollView.frame.size.height;
    CGFloat offsetX = scrollView.contentOffset.x;
    
    // 当前位置需要显示的控制器的索引
    NSInteger index = offsetX / width;
    
    // 取出需要显示的控制器
    UIViewController *willShowVc = self.childViewControllers[index];
    
    if (index == 0) {//我的
        //禁用我的按钮，颜色橙色
        self.meButton.enabled = NO;
        [self.meButton setTitleColor:HKColor(253, 151, 39) forState:UIControlStateDisabled];
        //安全宝下面的横条隐藏
        self.anquanbaoEditView.hidden = YES;
        //我的下面的横条变色，换图片
        self.meEditView.hidden = NO;
        self.meEditView.backgroundColor = HKColor(253, 151, 39);
        self.auquanbaoImageView.image = [UIImage imageNamed:@"40D6EF11-FE68-4E24-9AB1-A8BDFFED52BF"];
        //可用安全宝按钮
        self.anquanbaoButton.enabled = YES;
        //安全宝按钮白色
        [self.anquanbaoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }else {//安全宝
        //禁用安全宝按钮，颜色橙色
        self.anquanbaoButton.enabled = NO;
        [self.anquanbaoButton setTitleColor:HKColor(253, 151, 39) forState:UIControlStateDisabled];
        //我的下面的横条隐藏
        self.meEditView.hidden = YES;
        //安全宝下面的横条变色，换图片
        self.anquanbaoEditView.hidden = NO;
        self.anquanbaoEditView.backgroundColor = HKColor(253, 151, 39);
        self.auquanbaoImageView.image = [UIImage imageNamed:@"DC38CBE6-9589-4D9F-B70F-6604D07134C9"];
        //可用我的按钮
        self.meButton.enabled = YES;
        //我的按钮白色
        [self.meButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    
    // 如果当前位置的位置已经显示过了，就直接返回
    if ([willShowVc isViewLoaded]) return;
    
    // 添加控制器的view到contentScrollView中;
    willShowVc.view.frame = CGRectMake(offsetX, 0, width, height);
    [scrollView addSubview:willShowVc.view];
}

/**
 * 手指松开scrollView后，scrollView停止减速完毕就会调用这个
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self scrollViewDidEndScrollingAnimation:scrollView];
}


@end
